﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class course : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        initGridView();
    }
    void initGridView()
    {
        MySqlConnection con = DbUtils.getCon();
        MySqlCommand cmd = new MySqlCommand("select * from schedule", con);
        MySqlDataReader dr = cmd.ExecuteReader();
        string[,] schedules=new string[5,7];
        int numrows = 5;
        int numcells = 7;
        p2.InnerText = "";
        while (dr.Read())
        {
            string time = dr["time"].ToString();
            string[] times = time.Split(',');
            foreach(string str in times)
            {
                int date = int.Parse(str);
                int r = date / 7;
                int c = date % 7 - 1;
                schedules[r, c] = dr["cname"].ToString()+"<br/>"+dr["tname"];
                //p2.InnerHtml += schedules[r, c];
            }
            //int r = int.Parse();
            //p1.InnerText = time;
            //p2.InnerText = "";
            //foreach(string str in times)
            //{
            //    p2.InnerText += str;
            //}
            //p1.InnerText = time;
        }
        for (int i = 0; i < numrows; i++)
        {
            if (i == 2||i==4)
            {
                HtmlTableRow rowtemp = new HtmlTableRow();
                HtmlTableCell celltemp = new HtmlTableCell();
                celltemp.ColSpan = 8;
                celltemp.InnerText = i == 2 ? "午休时间" : "晚餐时间";
                rowtemp.Cells.Add(celltemp);

                table1.Rows.Add(rowtemp);
            }
            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell celltemp2 = new HtmlTableCell();
            celltemp2.InnerText = "第" + (i + 1) + "节";
            celltemp2.Style.Add("background-color", "#FFDEAD");
            row.Cells.Add(celltemp2);
            for (int j = 0; j < numcells; j++)
            {
                HtmlTableCell cell = new HtmlTableCell();
                if (schedules[i, j] == null)
                {
                    cell.Controls.Add(new LiteralControl("<br/><br/>"));
                }
                else
                {
                    cell.Controls.Add(new LiteralControl(schedules[i, j]));
                }
                row.Cells.Add(cell);
            }
            table1.Rows.Add(row);
        }
        //GridView1.DataSource = dr;
        //GridView1.DataBind();

        DbUtils.close(con, dr);
    }
}